<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once (dirname(__FILE__)."/../config/api.inc");
	$result['status'] = '0';
	
	//注册流程
	$phone =  getParam('phone','get');
	$code =  getParam('code','get');
	$pwd =  getParam('pwd','get');
	$yqm =  getParam('yqm','get');
	$source =  getParam('source','get');
	if(empty($phone))
	{
		$result['msg'] = '请输入手机号';
		echoJSON($result);
		exit;
	}
	
	if(empty($code))
	{
		$result['msg'] = '请输入验证码';
		echoJSON($result);
		exit;
	}
	
	if(empty($pwd))
	{
		$result['msg'] = '请输入密码';
		echoJSON($result);
		exit;
	}
	
	if(!empty($source))
	{
		unset($query);
		
		$query['id'] = base_convert($source,36,10);
		
		$info = _SqlTool::getInfo('users', $query);
		if(!empty($info))
		{
			$source = $info['id'];
		}
	}
	
	
	unset($where);
	
	$where['phone'] = $phone;
	$where['today'] = strtotime(date('Y-m-d'));
	if($uservalid = _User::getUserValidCode($where))
	{
		if($uservalid['errornums'] >= 5)
		{
			$result['msg'] = '验证码错误次数过多';
			echoJSON($result);
			exit;
		}
		
		if($uservalid['code'] == $code)
		{
			//删除验证码
			unset($query);
			$query['phone'] = $phone;
			_User::delUserValidCode($query);
			
			unset($data);
			$data['loginname'] = $phone;
			$data['password'] = md5($pwd);
			$data['regtime'] = time();
			$data['source'] = $source;
			$data['regip'] = getClientIP();
			$data['addtime'] = time();
			$res = _SqlTool::addInfo('users', $data);
			
			if(!empty($res))
			{
				$content = '亲爱的比赚用户您好~感谢您注册比赚，比赚登陆网址https://www.bigzhuan.com，微信公众号请搜索“比赚服务”，谢谢您的支持，祝生活愉快！';
				$url = 'http://push3.realsms.cn:38812/api/v10/send?usr=fumeng&pwd=qwer1234&src=188&dest='.$phone.'&msg='.$content;
				curl_send_post($url);
				
				//删除验证码
				unset($query);
				$query['phone'] = $phone;
				_User::delUserValidCode($query);
				
				$result['msg'] = '注册成功';
				$result['status'] = '1';
				
				unset($query,$userinfo);
				$query['id'] = $res;
				$userinfo = _SqlTool::getInfo('users', $query);
				
				unset($query,$data);
				$query['id'] = $userinfo['id'];
				$data['lastnums'] = $userinfo['lastnums'] + 1;
				$data['lasttime'] = time();
				$data['lastip'] = getClientIP();
				
				$data['loginkey'] = md5($data['lasttime'].$userinfo['id'].$data['lastnums'].$data['lastip'].'!@#$%^&*()!@#$EW');
				
				_SqlTool::setInfo('users', $query, $data);
				
				
				//写入session
				
				//$res
				unset($query,$userinfo);
				$query['id'] = $res;
				$userinfo = _SqlTool::getInfo('users',$query);
				
				
				$result['info'] = $userinfo;
				
				echoJSON($result);
				
			}
			else
			{
				$result['msg'] = '注册失败';
			}
		}
		else
		{
			unset($query,$data);
			$query['id'] = $uservalid['id'];
			$data['errornums'] = $uservalid['errornums']+1;
			
			_User::setUserValidCode($query, $data);
			$result['msg'] = '验证码输入错误';
			echoJSON($result);
			exit;
		}
	}
	else
	{
		$result['msg'] = '请先获取验证码';
		echoJSON($result);
		exit;
	}